package view;

import dao.User;
import dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/register")
public class blog_register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");

        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String password1=req.getParameter("password1");
        if(username==null||username.equals("")||password==null||password.equals("")||
                password1==null||password1.equals("")){
            String html="密码或用户名不正确";
            resp.getWriter().write(html);
            return;
        }
        if(!UserDao.selectByName(username).getUsername().equals("undefine")){
            String html="用户名已存在";
            resp.getWriter().write(html);
            return;
        }
        if(!password.equals(password1)){
            String html="两次密码不一样";
            resp.getWriter().write(html);
            return;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        UserDao.insert(user);
        resp.getWriter().write("<h3> 注册成功</h3>");

    }
}
